// global variables

var inactive = false;


// functions

function setInactive(flag)
{
  inactive = flag;
}

function restoreCurrentLanguage()
{
  try
  {
    var obj = document.getElementById("autolang");
    obj.setlang(parseInt(obj.title));
  }
  catch(e)
  {
    log("exception in RestoreCurrentLanguage " + e)
    return 0;
  }
}

function getCurrentLanguage()
{
  // check if current tab is active
  if(inactive) return;
  
  try
  {
    var obj = document.getElementById("autolang");
    var lcid = obj.getlang();
    obj.title = lcid.toString(); 
  }
  catch(e)
  {
    log("exception in GetCurrentLanguage " + e)
    return 0;
  }
}

// can be used for testing, if lcid is known
function setCurrentLanguage(lcid)
{
  try
  {
    var obj = document.getElementById("autolang");
    obj.setlang(lcid);
  }
  catch(e)
  {
    log("exception in SetCurrentLanguage " + e)
    return 0;
  }
}

// object injection
function checkForObject()
{
  var obj = document.getElementById("autolang");
  if(obj == null)
  {
    var d = document.createElement("object");
    d.id = "autolang";
    d.width = "1";
    d.height = "1";
    d.type = "application/x-auto-lang-keeper";
    d.innerHTML = "AUTOLANG";
    d.title = "1033";
    d.style.position = "absolute";
    d.style.left = "0px";
    d.style.top = "0px";
    d.style.zIndex = "0";
    d.style.opacity = "0.5"; // doesn't work
    document.getElementsByTagName("body")[0].appendChild(d);
  }
  setInterval(getCurrentLanguage, 1000);
}

// auxiliary shortcut for logging
function log(text)
{
  console.log(text);
}


checkForObject();
